package com.pifuke.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import cn.o.app.adapter.IItemView;
import cn.o.app.adapter.OAdapter;
import cn.o.app.adapter.OItemView;
import cn.o.app.json.JsonUtil;
import cn.o.app.ui.OImageView;
import cn.skinapp.R;

import com.lidroid.xutils.BitmapUtils;
import com.smiier.skin.DoctorDetailActivity;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.entity.RelationShip;
import com.smiier.skin.net.entity.UserWrapper;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;

public class SampleUserAdapter extends OAdapter<UserWrapper> {
	protected BitmapUtils mBitmapUtils;
	private boolean isDoctor = true;
	private BasicActivity mActivity;
	private Drawable mOffLine, mOnline, mTreatment;

	@Override
	protected void onContainerChanged() {
		Resources res = getContext().getResources();
		mOffLine = res.getDrawable(R.drawable.source_from_offline);
		mOnline = res.getDrawable(R.drawable.source_from_online);
		mTreatment = res.getDrawable(R.drawable.source_from_treatment);
		mOffLine.setBounds(0, 0, mOffLine.getIntrinsicWidth(),
				mOffLine.getIntrinsicHeight());
		mOnline.setBounds(0, 0, mOnline.getIntrinsicWidth(),
				mOnline.getIntrinsicHeight());
		mTreatment.setBounds(0, 0, mTreatment.getIntrinsicWidth(),
				mTreatment.getIntrinsicHeight());
		mBitmapUtils = new BitmapUtils(getContext());
	}

	public void setActivity(BasicActivity activity) {
		this.mActivity = activity;
	}

	@Override
	public IItemView<UserWrapper> getItemView() {
		return new DoctorUserView(getContext());
	}

	class DoctorUserView extends OItemView<UserWrapper> {
		OImageView img;
		TextView text_doctor_name, text_doctor_hospital, text_doctor_title;
		TextView mDoctorType;

		public DoctorUserView(Context context) {
			super(context);
		}

		@Override
		public void onCreate() {
			setContentView(R.layout.item_doctor_sample);
			img = findViewById(R.id.imgLayout_show, OImageView.class);
			text_doctor_name = findViewById(R.id.text_doctor_name,
					TextView.class);
			mDoctorType = findViewById(R.id.text_doctor_type, TextView.class);

			text_doctor_hospital = findViewById(R.id.text_doctor_hospital,
					TextView.class);
			text_doctor_title = findViewById(R.id.text_doctor_title,
					TextView.class);
			img.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getContext(),
							DoctorDetailActivity.class);
					try {
						intent.putExtra(Constant.IDENTITY_KEY,
								JsonUtil.convert(mDataProvider));
						getContext().startActivity(intent);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}

		@Override
		public void onResume() {
			try {
				text_doctor_name.setText(mDataProvider.User.Name);
				text_doctor_hospital.setText(mDataProvider.User.Hospital);
				if (isDoctor) {
					if (mDataProvider.RelationShip == null) {
						mDoctorType.setVisibility(View.GONE);
					} else {
						if (mDataProvider.RelationShip.RSSource == RelationShip.SOURCE_GENZONG) {
							mDoctorType.setText("项目跟踪");
							mDoctorType.setCompoundDrawables(null, null,
									mTreatment, null);
						} else if (mDataProvider.RelationShip.RSSource == RelationShip.SOURCE_SAOMIAO) {
							mDoctorType.setText("线下导入");
							mDoctorType.setCompoundDrawables(null, null,
									mOffLine, null);
						} else {
							mDoctorType.setText("在线咨询");
							mDoctorType.setCompoundDrawables(null, null,
									mOnline, null);
						}
						mDoctorType.setVisibility(View.VISIBLE);
					}
					List<String> prof = mDataProvider.User.Professional;
					StringBuilder sb = new StringBuilder();
					for (String str : prof) {
						sb.append(str).append(",");
					}
					if (sb.length() > 1) {
						text_doctor_title.setText(sb.replace(sb.length() - 1,
								sb.length(), "").toString());
					}
					text_doctor_hospital.setText(mDataProvider.User.Hospital);

				} else {
					text_doctor_title.setVisibility(View.GONE);
					mDoctorType.setVisibility(View.GONE);
					StringBuilder sb = new StringBuilder();
					String sex = CommonUtility.sex.get(mDataProvider.User.Sex);
					String age = CommonUtility
							.getAgeByBirthday(mDataProvider.User.Birth
									.toLocaleString());

					sb.append(sex);
					if (!CommonUtility.isNull(age)) {
						sb.append("，");
					}
					sb.append(age + "岁");
					Log.i("MyLog", sb.toString());
					text_doctor_hospital.setText(sb.toString());
				}
				String iconPath = mDataProvider.User.Pic;
				if (iconPath.indexOf(".") != -1) {
					iconPath = GlobalSettings.SERVER_IMG_URL + iconPath;
				} else {
					iconPath = GlobalSettings.SERVER_IMG_URL + iconPath
							+ Constant.IMG_TH;
				}
				// mBitmapUtils.display(img, iconPath,
				// new OBitmapLoadCallBack<View>());
				CommonUtility.displayHeadImage(img, mBitmapUtils, iconPath,
						getContext().getResources(), mDataProvider.User.Sex);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	public void setIsDoctor(boolean b) {
		isDoctor = b;

	}

}
